//主菜单左侧点击事件
function menu_click(categoryId) {
    sendJsonParam(categoryId, 1, null, null, null, null);
}

//商品详情查看事件
function product_detail(productID) {
    categoryId = $("#categoryId").text();
    window.location.href = "http://localhost/campus/product/detail?category=" + categoryId + "&productId=" + productID;
}

//发送ajax异步请求，异步更新UI
function sendJsonParam(categoryId, curPage, minPrice, maxPrice, productName, releaseTimeRange, isDisplayNav) {
    let jsonParam = {
        categoryId: categoryId
        , curPage: curPage
        , minPrice: minPrice
        , maxPrice: maxPrice
        , productName: productName
        , releaseTimeRange: releaseTimeRange
        , ajaxQuest: true
    };

    let stringParam = encodeURIComponent(JSON.stringify(jsonParam));

    if (isDisplayNav === undefined)
        isDisplayNav = true

    let param = isDisplayNav ? "" : "&isDisPlayNav=" + isDisplayNav;
    $.ajax({
        url: "http://localhost/campus/product/list"
        , type: "POST"
        , data: "condition=" + stringParam + param
        , success: function (data) {
            $('#center_body').html(data);
            element.render();
        }
    });
}

//商品添加收藏事件
function addFavorite(productId) {
    if (productId === "null")
        return false;
    $.ajax({
        type: "POST"
        , url: "http://localhost/campus/person/addFavorite"
        , data: "productId=" + productId
        , success: function (res) {
            let icon = res.success ? 1 : 2;
            layer.msg(res.message, {
                icon: icon
                , time: 2000
            });
        }
    });
}


//仿表单POST提交数据
function post(URL, PARAMS) {
    var temp = document.createElement("form");
    temp.action = URL;
    temp.method = "post";
    temp.style.display = "none";
    for (var x in PARAMS) {
        var opt = document.createElement("input");
        opt.name = x;
        opt.value = PARAMS[x];
        temp.appendChild(opt);
    }
    document.body.appendChild(temp);
    temp.submit();
    return temp;
}